package utils;

import java.util.*;

public class MapUtil {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap();
        map.put("张三", 5);


        List<Map.Entry<String, Integer>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
        String min = list.get(0).getKey();
        String max = list.get(list.size() - 1).getKey();

        System.out.println("最小的Key：" + min);
        System.out.println("最大的Key：" + max);
    }

    public static String getKeyWithMaxValue(HashMap<String,Integer> map){
        List<Map.Entry<String, Integer>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
        String min = list.get(0).getKey();
        return list.get(list.size() - 1).getKey();
    }

    public static String getKeyWithMinValue(HashMap<String,Integer> map){
        List<Map.Entry<String, Integer>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
        return list.get(0).getKey();
    }
}
